using System;
using System.Collections.Generic;
using System.Diagnostics;
using PugLib;

namespace ParallelTest
{
    public partial class Benchmarks
    {
        public static TestResult ListIntGenericsForEach()
        {
            Console.WriteLine("ListIntGenericsForEach");
            TestResult trd = new TestResult("ListIntGenericsForEach");
            Int64 total = 0;
            List<int> li = new List<int>(ItemCount);

            for (int i = 0; i < ItemCount; i++)
            {
                li.Add(1);
            }

            stopwatch.Reset();
            stopwatch.Start();
            for (int i = 0; i < Max; i++)
            {
                li.ForEach(delegate(int foo)
                {
                    total += foo;
                });
            }
            stopwatch.Stop();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;

            Debug.Assert(total == AssertValue);
            return trd;
        }

        public static TestResult ListIntForEach()
        {
            Console.WriteLine("ListIntForeach");
            TestResult trd = new TestResult("ListIntForeach");
            Int64 total = 0;
            List<int> li = new List<int>(ItemCount);

            for (int i = 0; i < ItemCount; i++)
            {
                li.Add(1);
            }

            stopwatch.Reset();
            stopwatch.Start();
            for (int i = 0; i < Max; i++)
            {
                foreach (int foo in li)
                {
                    total += foo;
                }
            }
            stopwatch.Stop();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;
            Debug.Assert(total == AssertValue);
            return trd;
        }

        public static TestResult ListIntFor()
        {
            Console.WriteLine("ListIntFor");
            TestResult trd = new TestResult("ListIntFor");
            Int64 total = 0;
            List<int> li = new List<int>(ItemCount);

            for (int i = 0; i < ItemCount; i++)
            {
                li.Add(1);
            }

            stopwatch.Reset();
            stopwatch.Start();
            for (int i = 0; i < Max; i++)
            {
                for (int iter = 0; iter < li.Count; iter++)
                {
                    total += li[iter];
                }
            }
            stopwatch.Stop();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;

            Debug.Assert(total == AssertValue);
            return trd;
        }

        public static TestResult ListToArray()
        {
            Console.WriteLine("ListToArray");
            TestResult trd = new TestResult("ListToArray");
            Int64 total = 0;
            List<int> li = new List<int>(ItemCount);

            for (int i = 0; i < ItemCount; i++)
            {
                li.Add(1);
            }

            stopwatch.Reset();
            stopwatch.Start();

            // convert to an array
            int[] arr = li.ToArray();

            for (int i = 0; i < Max; i++)
            {
                for (int iter = 0; iter < arr.Length; iter++)
                {
                    total += arr[iter];
                }
            }
            stopwatch.Stop();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;
            Debug.Assert(total == AssertValue);
            return trd;
        }

        public static TestResult ArrayFor()
        {
            Console.WriteLine("ArrayFor");
            TestResult trd = new TestResult("ArrayFor");
            Int64 total = 0;
            int[] li = new int[ItemCount];

            for (int i = 0; i < ItemCount; i++)
            {
                li[i] = 1;
            }

            stopwatch.Reset();
            stopwatch.Start();
            for (int i = 0; i < Max; i++)
            {
                for (int iter = 0; iter < li.Length; iter++)
                {
                    total += li[iter];
                }
            }
            stopwatch.Stop();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;
            Debug.Assert(total == AssertValue);
            return trd;
        }

        public static TestResult ArrayForEach()
        {
            Console.WriteLine("ArrayForEach");
            TestResult trd = new TestResult("ArrayForEach");
            Int64 total = 0;
            int[] li = new int[ItemCount];

            for (int i = 0; i < ItemCount; i++)
            {
                li[i] = 1;
            }

            stopwatch.Reset();
            stopwatch.Start();
            for (int i = 0; i < Max; i++)
            {
                foreach (int iter in li)
                {
                    total += iter;
                }
            }
            stopwatch.Stop();
            trd.Duration = stopwatch.Elapsed.TotalSeconds;
            Debug.Assert(total == AssertValue);
            return trd;
        }

    }
}
